package br.com.aerusc;

import java.util.logging.Logger;

import br.com.aerusc.enumerators.EnumMenu;
import br.com.aerusc.gui.components.MenuTree;
import br.com.aerusc.gui.components.RootComponents;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window;

public class AeruscMain extends Application {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Window mainWindow;
	private Tree menuTree;
	private HorizontalSplitPanel hsplit;
	private VerticalSplitPanel vsplit;
	private static final Logger log = Logger.getLogger(AeruscMain.class.getName());

	@Override
	public void init() {
		RootComponents.initialize(this);
		mainWindow = new Window("AERUSC - Rugby São Carlos");
		vsplit = new VerticalSplitPanel();
		vsplit.setSplitPosition(120, Sizeable.UNITS_PIXELS);
		vsplit.setLocked(true);
		hsplit = new HorizontalSplitPanel();
		Embedded em1 = new Embedded("", new ExternalResource("resources/img/juvenil.jpg"));
		em1.setMimeType("image/jpg");
		Embedded em2 = new Embedded("", new ExternalResource("resources/img/Saocarlos.png"));
		em2.setMimeType("image/jpg");
		HorizontalLayout horizontalLayout = new HorizontalLayout();
		horizontalLayout.setSizeFull();
		horizontalLayout.addComponent(em1);
		horizontalLayout.addComponent(em2);
		vsplit.setFirstComponent(horizontalLayout);
		vsplit.setSecondComponent(hsplit);
		hsplit.setSplitPosition(20, Sizeable.UNITS_PERCENTAGE);
		mainWindow.setContent(vsplit);
		menuTree = MenuTree.montarMenu();
		menuTree.addListener(new ItemClickListener() {

			@Override
			public void itemClick(ItemClickEvent event) {
				if (event.getSource() == menuTree) {
					EnumMenu itemId = (EnumMenu) event.getItemId();
					if (itemId != null) {
						Panel panel = MenuTree.getSelectPanel(itemId);
						if (panel != null) {
							if (!panel.equals(hsplit.getSecondComponent())) {
								hsplit.setSecondComponent(panel);
							}
						}
					}
				}
			}
		});
//		Codigo referente a navegação pelo teclado, porém como cada aba tem um .focus(), não vale a pena deixar.
//		caso utilizar, colocar como immediate a Tree.
//		menuTree.addListener(new Property.ValueChangeListener() {
//
//			@Override
//			public void valueChange(ValueChangeEvent event) {
//				EnumMenu itemId = (EnumMenu) event.getProperty().getValue();
//				if (itemId != null) {
//					Panel panel = MenuTree.getSelectPanel(itemId);
//					if (panel != null) {
//						if (!panel.equals(hsplit.getSecondComponent())) {
//							hsplit.setSecondComponent(panel);
//						}
//					}
//				}
//
//			}
//		});
		hsplit.setFirstComponent(menuTree);
		RootComponents.setHorizontalSplitLayout(hsplit);
		RootComponents.setMainWindow(mainWindow);
		setMainWindow(mainWindow);
	}
}
